package day03;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * <a href="https://leetcode-cn.com/problems/add-two-numbers/">2. 两数相加</a>
 */
public class Answer {

    public static void main(String[] args) {
        Answer answer = new Answer();

        ListNode l1 = new ListNode();
        l1.val = 9;


        ListNode l2 = new ListNode();
        l2.val = 1;
        ListNode l3 = new ListNode();
        l3.val = 9;
        ListNode l4 = new ListNode();
        l4.val = 9;
        ListNode l5 = new ListNode();
        l5.val = 9;
        ListNode l6 = new ListNode();
        l6.val = 9;
        ListNode l7 = new ListNode();
        l7.val = 9;
        ListNode l8 = new ListNode();
        l8.val = 9;
        ListNode l9 = new ListNode();
        l9.val = 9;
        ListNode l10 = new ListNode();
        l10.val = 9;
        ListNode l11 = new ListNode();
        l11.val = 9;

        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        l6.next = l7;
        l7.next = l8;
        l8.next = l9;
        l9.next = l10;
        l10.next = l11;

        ListNode listNode = answer.addTwoNumbers(l1, l2);
        System.out.println(listNode);
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode();
        StringBuilder s = new StringBuilder();
        int c = 0;
        while (l1 != null || l2 != null) {
            int a = l1 != null ? l1.val : 0;
            int b = l2 != null ? l2.val : 0;

            int num;
            int total = a + b + c;
            if (total >= 10) {
                c = 1;
                num = total - 10;
            } else {
                c = 0;
                num = total;
            }
            s.append(num);
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        if (c > 0) {
            s.append(c);
        }
        return crateListNode(listNode, s.reverse().toString(), s.length() - 1);
    }

    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        String num1 = sum(l1, 0);
        String num2 = sum(l2, 0);
        ListNode listNode = new ListNode();
        StringBuilder s = new StringBuilder();
        int length = Math.max(num1.length(), num2.length());
        int c = 0;
        for (int i = 0; i <= length; i++) {
            int a = 0,b = 0;
            try {
                a = Integer.parseInt(num1.substring(i, i+1));
            } catch (Exception e) {

            }
            try {
                b = Integer.parseInt(num2.substring(i, i+1));
            } catch (Exception e) {

            }
            int num;
            int total = a + b + c;
            if (total >= 10) {
                c = 1;
                num = total - 10;
            } else {
                c = 0;
                num = total;
            }
            if (total == 0 && i == length) {
                continue;
            }
            s.append(num);
        }
        return crateListNode(listNode, s.reverse().toString(), s.length() - 1);
    }

    public ListNode crateListNode(ListNode listNode, String total, int num) {
        if (num >= 0) {
            listNode.val = Integer.parseInt(String.valueOf(total.charAt(num)));
            listNode.next = crateListNode(new ListNode(), total, --num);
        } else {
            listNode = null;
        }
        return listNode;
    }

    public String sum(ListNode listNode, int num) {
        String total = String.valueOf(listNode.val);
        if (listNode.next != null) {
            total += sum(listNode.next, ++num);
        }
        return total;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            this.val = x;
        }

        public ListNode() {
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public int getVal() {
            return val;
        }

        @Override
        public String toString() {
            return new StringJoiner(", ", ListNode.class.getSimpleName() + "[", "]")
                    .add("val=" + val)
                    .add("next=" + next)
                    .toString();
        }
    }

}